//
//  ZCStatusTableViewCell.swift
//  weibo
//
//  Created by Ning Xie on 16/8/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit


let StatusTableViewCellMargin: CGFloat = 10

class ZCStatusTableViewCell: UITableViewCell {
 
    
    var statusToolBarConstraint:Constraint?
    //  处理微博数据的ViewModel
    var statusViewModel: ZCStatusViewModel? {
        didSet {
            //  设置数据
            originView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            
            statusToolBarConstraint?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
                
                retweetView.hidden = false
                
                retweetView.statusViewModel = statusViewModel
                
                //更新约束
                statusToolBar.snp_updateConstraints(closure: { (make) in
                    self.statusToolBarConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else{
                retweetView.hidden = true
                statusToolBar.snp_updateConstraints(closure: { (make) in
                    self.statusToolBarConstraint = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
            
            
        }
    }
    
    //懒加载原创微博视图
    private lazy var originView:ZCStatusOriginView = ZCStatusOriginView()
    
    //懒加载转发微博视图
    private lazy var retweetView:ZCStatusRetweetView = ZCStatusRetweetView()
    
    
    
    //懒加载微博工具条
    private lazy var statusToolBar:ZCStatusToolBar = ZCStatusToolBar()
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .None
        setupUI()

    }
  
    
    //布局视图
    private func setupUI(){
        
        contentView.addSubview(originView)
        contentView.addSubview(retweetView)
        contentView.addSubview(statusToolBar)
        
        originView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(contentView)
        }
        retweetView.snp_makeConstraints { (make) in
            make.top.equalTo(originView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        
        statusToolBar.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(35)
            self.statusToolBarConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
        }
       
        contentView.snp_makeConstraints { (make) in
            make.bottom.equalTo(statusToolBar)
            make.top.leading.trailing.equalTo(self)
        }
        
    }
    

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
